<template>
  <div>
    <el-dialog
      class="dialog"
      :title="LabelID?'编辑学科':'新增学科'"
      :visible="addVisible"
      width="40%"
      @close="addCancel"
    >
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="所属学科" prop="subjectName">
          <el-select v-model="ruleForm.subjectName" placeholder="请选择" @change="selectData($event)">
            <el-option
              v-for="(item) in LabelTableList"
              :key="item.id"
              :label="item.subjectName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="标签名称" prop="labelname">
          <el-input v-model="ruleForm.labelname" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addLabelTagsAPI, UpdateLabelTagsAPI } from '@/api'
export default {
  props: {
    addVisible: Boolean,
    // eslint-disable-next-line vue/prop-name-casing
    LabelTableList: {
			type: Array,
			default: () => {}
		},
    // eslint-disable-next-line vue/prop-name-casing
    LabelID: {
			type: [String, Number],
			default: () => {}
			},
    updateList: {
			type: Object,
			default: () => {}
		}
  },
  data() {
    return {
      ruleForm: {
        subjectName: '',
        labelname: ''
      },
      rules: {
        labelname: [
          { required: true, message: '请输入标签名称', trigger: 'blur' }
        ],
        subjectName: [
          { required: true, message: '请选择所属学科', trigger: 'blur' }
        ]
      },
      ID: ''
    }
  },
  watch: {
    updateList: {
      handler(value) {
        if (value) {
          console.log(value);
          this.ruleForm.subjectName = value.subjectName
          this.ruleForm.labelname = value.tagName

        } else {
          this.ruleForm.subjectName = ''
          this.ruleForm.labelname = ''
        }
      },
      immediate: true,
      deep: true
    }
  },

  methods: {

    /**
    * change事件 获取id
    */
    selectData(result) {
      this.ID = result
    },


    /**
     * 确定 添加 标签
     */
    confirm() {
      if (this.LabelID) {
        /** 有id 为编辑 */
        this.editLable()
      } else {
        this.addLable()
      }
    },

    /**
     * 编辑标签
     */
    async editLable() {
			console.log(this.updateList);
      const res = await UpdateLabelTagsAPI(this.updateList.id, this.updateList.subjectID, this.ruleForm.labelname)
      console.log(res);
      // 关闭弹窗
      this.$emit('update:addVisible', false)
			this.$emit('updateLabelList')// 子传父在传父，刷新页面
      // 提示用户
      this.$message.success('添加标签成功')

    },


    /**
     * 添加标签
     */
    addLable() {
      this.$refs.ruleFormRef.validate(async(isok) => {
        if (!isok) return
        const res = await addLabelTagsAPI(this.ID, this.ruleForm.labelname)
        console.log('这是增加标签');

        // 关闭弹窗
        this.$emit('update:addVisible', false)
        // 提示用户
        this.$message.success('添加标签成功')

      })
    },
		/**
		 * 点击取消，清空列表中的数据，关闭弹层
		 * **/
		addCancel() {
			// 清空数据
			this.ruleForm = {
        subjectName: '',
        labelname: ''
      }
			this.$emit('update:addVisible', false)
		}
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__footer{
	margin-top: 150px;
}
::v-deep .el-dialog{
	border-radius: 15px;
}
::v-deep .el-dialog__header{
	background-color:#409eff;
	border-radius: 15px 15px 0 0;

}
::v-deep .el-dialog__title{
	color:#fff;
}
::v-deep .el-dialog__close{
	color:#fff
}
</style>
